<?php
/*
****商品展示页
*/
class GoodsAction extends Action
{

	public function item()
	{
		$Goods=M('Goods');
		$Goods_pic=M('Goods_pic');
		$Ads=M('Ads');
		$Brand=M('Goods_brand');
		$Page=M('Page_zhinan');

		$id = intval($this->_get('id'));
		$map['goods_id']=$id;
		$this->pic=$Goods_pic->where($map)->select();
		$goods=$Goods->where('goods_id='.$id)->find();
		if(false==$goods||$goods['is_post']==1){
			$this->redirect('Public/error.html');
		}
		$brand = $Brand->where('brand_id='.$goods['goods_brand'])->find();
		$this->assign("brand",$brand);
		$this->shouhou=$Page->where("title='售后服务'")->find();

		$prop=json_decode($goods['props']);
		$prop_num=count($prop);
		switch($prop_num){
			case 1:
				$prop1=get_object_vars($prop[0]);
			    $this->prop1_n=$prop1['prop_name']."：";
				$this->prop1=$prop1['prop_value'];
			break;
			case 2:
				$prop1=get_object_vars($prop[0]);
			    $this->prop1_n=$prop1['prop_name']."：";
				$this->prop1=$prop1['prop_value'];

				$prop2=get_object_vars($prop[1]);
			    $this->prop2_n=$prop2['prop_name']."：";
				$this->prop2=$prop2['prop_value'];
			break;
		}
		
		if($goods['props']==''){
			$Product=M('Products');
			$pro=$Product->where('goods_id='.$id)->find();
			$this->product_id=$pro['product_id'];
		}
        
		$url=$_SERVER['HTTP_REFERER'];

		//商品评价
		$this->comment($id);

        $tj_goods['goods_label']='hot';
		$this->tuijian=$Goods->where($tj_goods)->limit(5)->select();

		//商品属性
		$this->assign("pdt",unserialize($goods['pdt']));


        R('Public/goods_view',array($id));//商品浏览cookie
		R('View/goods_count',array($id,$url));//
		$this->ads=$Ads->where("place_tag='Item01' and is_show='true'")->order('sorting')->limit(2)->select();
		$this->cat=$this->goods_cat();

		$this->name=$goods['goods_name'].'-'.C('WEB_NAME');
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->assign("goods",$goods);
		$this->user=$_SESSION['euname'];
		$this->display();
	}
	//item结束

	//商品分类
	public function goods_cat(){
		$Cat=M('Goods_cat');
		$cat=$Cat->where('is_active=0 and pid=0')->order('sorting')->select();
		for($i=0;$i<count($cat);$i++){
			//推荐分类
			$map['pid'] = $cat[$i]['cat_id'];
			$map['is_active'] = 0;
			$map['is_hot'] = 1;
			$cat[$i]['rec'] = $Cat->where($map)->order('sorting')->limit(3)->select();
			//获取二级分类
			$son=$Cat->where('is_active=0 and pid='.$cat[$i]['cat_id'])->order('sorting')->select();
			$cat[$i]['son']=$son;
			//获取三级分类
			for($j=0;$j<count($cat[$i]['son']);$j++){
				$gson=$Cat->where('is_active=0 and pid='.$cat[$i]['son'][$j]['cat_id'])->order('sorting')->select();
			    $cat[$i]['son'][$j]['gson']=$gson;
			}
		}
		return $cat;
	}

	public function get_price(){
		$Products =M('Products');
		$prop1=$this->_post('prop1');
		$prop2=$this->_post('prop2');
		if(!empty($prop2)){
		   $prop=$prop1.",".$prop2;
		}else{
			$prop=$prop1;
		}
		$id=$this->_post('id');
		$map['goods_id']=$id;
		$map['props']=$prop;
		$price=$Products->where($map)->find();

		$fan=array("price"=>$price['price'],"store"=>$price['store'],"choose_id"=>$price['product_id']);
		echo json_encode($fan);
	}

	//获取商品介绍内容
	public function ajax_intro(){
		$Goods=M('Goods');
		$Global=M('Goods_global');
		$id = intval($this->_get('id'));
		$goods=$Goods->where('goods_id='.$id)->find();

		preg_match_all("/{TAG:[A-Z0-9]*}/",$goods['intro'],$a);
		for($i=0;$i<count($a[0]);$i++){
			$map['global_key']=$a[0][$i];
			$g_variable=$Global->where($map)->find();
		    $goods['intro']=str_replace($a[0][$i],$g_variable['global_value'],$goods['intro']);
		}
		echo $goods['intro'];
	}

	//商品咨询
	public function comment($goods_id){
		$map['e_products.goods_id']=$goods_id;
		//商品咨询
		$Comm=M('Goods_comment');
	    import("@.ORG.Page");
		$comm=$Comm->field('e_member.member_id,e_member.uname,e_member.u_pic,e_goods_comment.*,e_products.*')->join('e_member ON e_goods_comment.member_id=e_member.member_id')->join('e_products ON e_products.product_id=e_goods_comment.product_id')->where($map)->order('e_goods_comment.com_time desc')->select();
		$param = array(
			'result'=>$comm,			//分页用的数组或sql
			'listvar'=>'comm',			//分页循环变量
			'listRows'=>2,			//每页记录数
			'parameter'=>'',//url分页后继续带的参数
			'target'=>'commlist',	//ajax更新内容的容器id，不带#
			'pagesId'=>'page',		//分页后页的容器id不带# target和pagesId同时定义才Ajax分页
			'template'=>'Goods:commlist',//ajax更新模板
		);
		$this->page($param);
	}

	public function page($param) {
		extract($param);
		import("@.ORG.Page");
		//总记录数
		$flag = is_string($result);
		$listvar = $listvar ? $listvar : 'comm';
		$listRows = $listRows? $listRows : 21;
		if ($flag)
			$totalRows = M()->table($result . ' a')->count();
		else
			$totalRows = ($result) ? count($result) : 1;
		//创建分页对象
		if ($target && $pagesId)
			$p = new Page($totalRows, $listRows, $parameter, $url,$target, $pagesId);
		else
			$p = new Page($totalRows, $listRows, $parameter,$url);
		//抽取数据
		if ($flag) {
			$result .= " LIMIT {$p->firstRow},{$p->listRows}";
			$voList = M()->query($result);
		} else {
			$voList = array_slice($result, $p->firstRow, $p->listRows);
		}
		$pages = C('PAGE');
		foreach ($pages as $key => $value) {
			$p->setConfig($key, $value); // 'theme'=>'%upPage% %linkPage% %downPage% %ajax%'; 要带 %ajax%
		}
		//分页显示
		$page = $p->show();
		//模板赋值
		$this->assign($listvar, $voList);
		$this->assign("page", $page);

		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template = (!$template) ? 'commlist' : $template;
			exit($this->fetch($template));
		}
		return $voList;
	}

	public function goods_store(){
		$Member=M('Member');
		$Store=M('Member_store');
		$map['goods_id']= intval($this->_get('goods_id'));
		$name=$_SESSION['euname'];
		if(!isset($name)){
			echo 1;
		}else{
			$map2['uname']=$name;
			$member=$Member->where($map2)->find();
			$map['member_id']=$member['member_id'];
			if($Store->where($map)->select()){
				echo 2;
			}else{
				$map['add_time']=time();
				$Store->add($map);
				echo 3;
			}
		}
	}

	public function updateCart(){
		$shop_cart=cookie('shopcart');
		$cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		echo $cart_num;
	}

    //商品评论
    public function goodComm(){
		$Order=M('Orders');
		$Item=M('Orders_item');
		$Member=M('Member');
		$now_mem=$_SESSION['euname'];

		$order_id = htmlspecialchars($this->_get('order'));
		$ck['order_id']=$order_id;
		$order=$Order->where($ck)->find();
		$ck_m['member_id']=$order['member_id'];
		$member=$Member->where($ck_m)->find();
		if($order['status']!='finish'){
			$this->error("此订单当前状态不允许评价！","__APP__/Member/trade");
		}
		if($member['uname']!=$now_mem){
			$this->error("无权操作！","__APP__");
		}
		if($order['finish_comm']==1){
			$this->error("您已经评价过了！","__APP__/Member/trade");
		}

		$map['e_orders_item.order_id']=$order_id;
		$this->order_item=$Item->join('e_products ON e_orders_item.product_id=e_products.product_id')->join('e_goods ON e_goods.goods_id=e_products.goods_id')->where($map)->select();
		$this->order=$order;

		$this->user=$now_mem;
		$this->cat=R('Goods/goods_cat');
		$this->name="商品评价";
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->display();
	}

	public function dogoodComm(){
		$Member=M('Member');
		$Comm=M('Goods_comment');
		$Order=M('Orders');

		$map['uname']=$_SESSION['euname'];
		$member=$Member->where($map)->find();

		$data['order_id'] = htmlspecialchars($this->_get('order'));
		$product=$this->_post('product');
		$comment=$this->_post('comm');
		$data['com_time']=time();
		$data['member_id']=$member['member_id'];

		foreach($product as $key=>$pid){
			$data['product_id']=$pid;
			$data['level']=$this->_post('level'.$pid);
			$data['comment']=$comment[$key];
			$Comm->add($data);
		}
		$data2['finish_comm']=1;
		$map2['order_id']=$data['order_id'];
		$Order->where($map2)->save($data2);
		$this->success("评价成功！","/");
    }

}
?>